package test0701;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class PsAdd {

	public static void main(String[] args) {
		// 3:在控制台输入一个学生的全部信息，保存到数据库，注意日期的处理(PreparedStatement)
		Connection connection = null;
		PreparedStatement pstmt = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/myschool?useSSL=false";
			connection = DriverManager.getConnection(url, "root", "zy940425");
			String sql = "insert into s_student(sname,sex,telephone,gradeid,fromcity,birthday) values(?,?,?,?,?,?)";
			pstmt = connection.prepareStatement(sql);
			Scanner input = new Scanner(System.in);
			System.out.print("请输入学生姓名：");
			String sname = input.next();
			System.out.print("请输入学生性别：");
			String sex = input.next();
			System.out.print("请输入学生电话：");
			String telephone = input.next();
			System.out.print("请输入学生年级：");
			int gradeid = input.nextInt();
			System.out.print("请输入学生家乡：");
			String fromcity = input.next();
			System.out.print("请输入学生生日（格式为：2020-7-2）：");
			String birthday = input.next();
			pstmt.setString(1, sname);
			pstmt.setString(2, sex);
			pstmt.setString(3, telephone);
			pstmt.setInt(4, gradeid);
			pstmt.setString(5, fromcity);
			pstmt.setString(6, birthday);

//			pstmt.setString(1, "王二狗");
//			pstmt.setString(2, "男");
//			pstmt.setString(3, "13811111112");
//			pstmt.setString(4, "2");
//			pstmt.setString(5, "阜阳");
//			pstmt.setString(6, "2020-7-2");

			System.out.println(pstmt.executeUpdate() + "条增加成功！");

		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null)
					pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (connection != null)
					connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
